package cn.tedu.service.impl;

import cn.tedu.mapper.WxUserinfoMapper;
import cn.tedu.pojo.dto.WxUserinfoDTO;
import cn.tedu.pojo.entity.WxUserinfo;
import cn.tedu.pojo.vo.PageInfo;
import cn.tedu.pojo.vo.WxUserinfoVo;
import cn.tedu.service.IWxUserinfoService;
import cn.tedu.util.PojoConvert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * I`m 实现类
 * @author Kiven
 */
@Service
public class WxUserinfoServiceImpl
        extends ServiceImpl<WxUserinfoMapper, WxUserinfo>
        implements IWxUserinfoService {

    @Autowired
    private WxUserinfoMapper wxUserinfoMapper;

    @Override
    public PageInfo<WxUserinfoVo> findWxUserinfos(WxUserinfoDTO wxUserinfoDTO,Long pageCurrent,Long size) {
        Page<WxUserinfo> page = new Page<>(pageCurrent,size);

        // 动态sql 分页查询
        IPage<WxUserinfo> wxUserinfoIPage = wxUserinfoMapper.selectPage(page,
                Wrappers.<WxUserinfo>lambdaQuery()
                        .eq(StringUtils.isNotBlank(wxUserinfoDTO.getNickname()),WxUserinfo::getNickname,wxUserinfoDTO.getNickname())
                        .eq(wxUserinfoDTO.getSex()!=null,WxUserinfo::getSex,wxUserinfoDTO.getSex())
                        .eq(StringUtils.isNotBlank(wxUserinfoDTO.getCity()),WxUserinfo::getCity,wxUserinfoDTO.getCity())
                        .orderByDesc(WxUserinfo::getCity)
        );

        PageInfo<WxUserinfoVo> pageInfo = new PageInfo<>();
        pageInfo.setPageCurrent(String.valueOf(wxUserinfoIPage.getCurrent()));
        pageInfo.setPageCount(String.valueOf(wxUserinfoIPage.getPages()));
        pageInfo.setRowCount(String.valueOf(wxUserinfoIPage.getTotal()));

        System.out.println(wxUserinfoIPage.getRecords());
        // 将查询结果转换为VO
        List<WxUserinfo> records = wxUserinfoIPage.getRecords();
        List<WxUserinfoVo> wxUserinfoVos =
                PojoConvert.convertList(records, WxUserinfoVo.class);

        pageInfo.setRecords(wxUserinfoVos);
        return pageInfo;
    }
}
